<?php

class huyen extends database
{
    private $_ten_huyen;
    private $_tinh_id;
    
    public function __construct()
    {
        $this->connection();
    }
    
    public function setTen_huyen($name)
    {
        $this->_ten_huyen = $name;
    }
    
    public function getTen_huyen()
    {
        return $this->_ten_huyen;
    }
    
    public function setTinh_id($id)
    {
        $this->_tinh_id = $id;
    }
    
    public function getTinh_id()
    {
        return $this->_tinh_id;
    }
    
    function get_auto_increment()
    {
        $sql = "SHOW TABLE STATUS WHERE name = 'huyen'";
        $this->query($sql);
        if ($this->num_rows() != 0) {
            $row = $this->fetch();
            return $row['Auto_increment'];
        } else {
            return FALSE;
        }
    }
    
    public function insert_huyen()
    {
        $sql = "INSERT INTO huyen(tinh_id, ten_huyen) VALUES('" . $this->getTinh_id() . "', '" . $this->getTen_huyen() . "')";
        $this->query($sql);
    }
    
    public function edit_huyen($id)
    {
        $sql = "UPDATE huyen SET ten_huyen=N'" . $this->getTen_huyen() . "', tinh_id=N'" . $this->getTinh_id() . "' WHERE id=N'" . $id . "'";
        $this->query($sql);
    }
    
    public function delete_huyen($id)
    {
        $sql = "DELETE FROM huyen WHERE id='" . $id . "'";
        $this->query($sql);
    }
    
    
    public function total_num_rows()
    {
        $sql = "SELECT * FROM huyen";
        $this->query($sql);
        $total_num_rows = $this->num_rows();
        return $total_num_rows;
    }
    
    public function getbyid($id)
    {
        $sql = "SELECT * FROM huyen WHERE id='" . $id . "'";
        $this->query($sql);
        if ($this->num_rows() != 0) {
            $row = $this->fetch();
            return $row;
        } else {
            return FALSE;
        }
    }
    
    public function getbyoption($options = array())
    {
        $select   = isset($options['select']) ? $options['select'] : '*';
        $where    = isset($options['where']) ? 'WHERE' . $options['where'] : '';
        $order_by = isset($options['order_by']) ? 'ORDER BY ' . $options['order_by'] : '';
        $limit    = isset($options['offset']) && isset($options['limit']) ? 'LIMIT ' . $options['offset'] . ',' . $options['limit'] : '';
        $sql      = "SELECT $select FROM huyen $where $order_by $limit";
        $this->query($sql);
        if ($this->num_rows() != 0) {
            while ($row = $this->fetch()) {
                $data[] = $row;
            }
            return $data;
        } else {
            return FALSE;
        }
    }
}